package com.crayfish.quduo.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpFragment;
import com.crayfish.quduo.commom.Params;
import com.crayfish.quduo.http.RetrofitHelper;
import com.crayfish.quduo.model.BannerBean;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.model.LaiFuDaoBean;
import com.crayfish.quduo.presenter.MainPresenter;
import com.crayfish.quduo.presenter.contract.MainContract;
import com.crayfish.quduo.ui.activity.ImageActivity;
import com.crayfish.quduo.ui.adapter.EndlessRecyclerOnScrollListener;
import com.crayfish.quduo.ui.adapter.FooterHolder;
import com.crayfish.quduo.ui.adapter.JokeListAdapter;
import com.crayfish.quduo.ui.adapter.LaiFuDaoAdapter;
import com.crayfish.quduo.utils.ToastUtil;
import com.crayfish.quduo.widget.Banner.Banner;
import com.crayfish.quduo.widget.Banner.BannerAdapter;
import com.crayfish.quduo.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bmob.v3.update.BmobUpdateAgent;

/**
 * Created by crayfish on 2016/11/24.
 */
public class HomeFragment extends BaseMvpFragment<MainPresenter> implements MainContract.View{

    @BindView(R.id.banner)
    Banner banner;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    public static final String VIEW_NAME_IMAGE = "VIEW_NAME_IMAGE";
    public static final String VIEW_TITLE = "VIEW_TITLE";

    private Boolean isRefresh = false;
    BannerAdapter bannerAdapter;
    LaiFuDaoAdapter laiFuDaoAdapter;
    JokeListAdapter jokeListAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(new RetrofitHelper());
    }

    @Override
    protected void initEventAndData() {
        BmobUpdateAgent.update(getActivity());
        swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.GREEN,Color.YELLOW,Color.RED);
        swipeRefreshLayout.setDistanceToTriggerSync(300);
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

                            //显示或隐藏刷新进度条
                            swipeRefreshLayout.setRefreshing(false);
                            //修改adapter的数据
                            mPresenter.getJokeData();
                            isRefresh = false;
                        }
                    }, 3000);
                }
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(),LinearLayoutManager.VERTICAL));
        recyclerView.setHasFixedSize(true);//固定高度提高性能
        laiFuDaoAdapter = new LaiFuDaoAdapter(getActivity());
        jokeListAdapter = new JokeListAdapter(getActivity());
        jokeListAdapter.setOnRecyclerViewItemClickListener(new JokeListAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, JokeItemBean.Item item) {
                Intent intent = new Intent(getActivity(), ImageActivity.class);
                intent.putExtra(Params.JOKE_ITEM,item);
//                ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
//                        getActivity(),new Pair<View, String>(v.findViewById(R.id.iv_cover_pic),HomeFragment.VIEW_NAME_IMAGE),
//                        new Pair<View, String>(v.findViewById(R.id.txt_title),HomeFragment.VIEW_TITLE)
//                );
//                ActivityCompat.startActivity(getActivity(),intent,activityOptionsCompat.toBundle());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(jokeListAdapter);//替换Adapter
        bannerAdapter = new BannerAdapter<BannerBean>() {
            @Override
            protected void bindTips(TextView tv, BannerBean bannerBean) {
                tv.setText(bannerBean.getBanner_title());
            }

            @Override
            public void bindImage(ImageView imageView, BannerBean bannerBean) {
                Glide.with(getActivity())
                        .load(bannerBean.getBanner_img())
                        .placeholder(R.mipmap.empty)
                        .error(R.mipmap.error)
                        .into(imageView);
            }
        };
        banner.setBannerAdapter(bannerAdapter);
//        mPresenter.getTupianData();
        mPresenter.getJokeData();
        mPresenter.getBannerData();

    }

    @Override
    public void showError(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void showContent(List<LaiFuDaoBean> laiFuDaoBeen) {
        if(laiFuDaoBeen.size() > 0){
            laiFuDaoAdapter.clear();
            laiFuDaoAdapter.addAll(laiFuDaoBeen);
        }
    }

    @Override
    public void showBanner(List<BannerBean> bannerBeen) {
        if(bannerBeen.size() > 0){
            bannerAdapter.addAll(bannerBeen);
            banner.notifiDataHasChanged();
        }
    }

    @Override
    public void showContents(List<JokeItemBean.Item> jokeItemBeen) {
        if(jokeItemBeen.size() > 0){
            jokeListAdapter.clear();
            jokeListAdapter.addAll(jokeItemBeen);
        }
        setState();
    }

    protected void setState() {
        ((getActivity())).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                changeAdapterState();
            }
        });
    }

    //改变底部bottom的样式
    protected void changeAdapterState() {
        if (jokeListAdapter != null && jokeListAdapter.footerHolder != null) {
            jokeListAdapter.footerHolder.setStatus(FooterHolder.TheEnd);
        }
    }
}
